package com.tsugun.mediaplayer.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.model.entity.MusicModel;
import com.tsugun.mediaplayer.model.entity.SheetModel;
import com.tsugun.mediaplayer.presenter.provider.ListProvider;
import com.tsugun.mediaplayer.view.loader.MediaImageLoad;

import java.util.List;

/**
 * 列表适配器
 * Created by shize on 2017/3/30.
 */

public class SheetItemMenuAdapter extends RecyclerView.Adapter<SheetItemMenuAdapter.RecyclerViewHolder> {

    private static final String COUNT_AGES = "首歌曲";

    private LayoutInflater mInflater;
    private List<SheetModel> mData;
    private OnSheetItemClickListener mOnSheetItemClickListener;
    private ListProvider mProvider;
    private List<String> mMusicList;

    public interface OnSheetItemClickListener {
        void onItemClick(int position);
    }

    public SheetItemMenuAdapter(Context mContext, List<SheetModel> mData) {
        this.mData = mData;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setOnSheetItemClickListener(OnSheetItemClickListener OnSheetItemClickListener) {
        mOnSheetItemClickListener = OnSheetItemClickListener;
    }

    public void setProvider(ListProvider provider) {
        mProvider = provider;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RecyclerViewHolder(mInflater.inflate(R.layout.item_sheet_menu, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
        mMusicList = mData.get(position).getMusicIdList();
        String strCount = mMusicList.size() + COUNT_AGES;
        holder.mIvIcon.setImageResource(R.drawable.ic_music_default);
        // 默认加载第一首音乐专辑的图片
        setSheetIcon(holder, position);
        holder.mTvTitle.setText(mData.get(position).getTitle());
        holder.mTvCount.setText(strCount);
        if (mOnSheetItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnSheetItemClickListener.onItemClick(holder.getAdapterPosition());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    /**
     * 默认加载第一首音乐专辑的图片
     */
    private void setSheetIcon(SheetItemMenuAdapter.RecyclerViewHolder holder, int position) {
        if (mData.get(position).getImageUrl() != null || mProvider == null) {
//            MediaImageLoad.localMusicImageLoad(mData.get(position).getImageUrl(), holder.mIvIcon);
            return;
        }
        if (mMusicList.size() < 1) {
            return;
        }
        MusicModel musicModel = mProvider.getMusic(mMusicList.get(0));
        if (musicModel != null) {
            MediaImageLoad.localImageLoad(musicModel,holder.mIvIcon);
        }
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder {

        ImageView mIvIcon;
        TextView mTvTitle;
        TextView mTvCount;

        RecyclerViewHolder(View itemView) {
            super(itemView);
            mIvIcon = (ImageView) itemView.findViewById(R.id.id_item_sheet_image);
            mTvTitle = (TextView) itemView.findViewById(R.id.id_item_sheet_title);
            mTvCount = (TextView) itemView.findViewById(R.id.id_item_sheet_sub_title);
        }
    }
}
